﻿Public Class Group
    Public l As Int16 = 0
    Public cbo As Int16 = 0
    Private Sub btnnew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnew.Click
        Dim frm As New AddGroup
        frm.ShowDialog()
    End Sub

    Private Sub Group_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadgroup()
        If cbo > 0 Then cbogroup.SelectedValue = cbo
    End Sub
    Private Sub loadgroup()
        cbogroup.ValueMember = "User_Type_id"
        cbogroup.DisplayMember = "User_Type"
        cbogroup.DataSource = cmd.ExecuteDataTable("SELECT User_Type_Id,User_Type FROM User_Type_Tbl ORDER BY User_Type_id DESC")
    End Sub

    Private Sub btnok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnok.Click
        Dim a, b, c, d As Int16
        a = 0
        b = 0
        c = 0
        d = 0
        If cbemp.Checked Then
            a = 1
        End If
        If cbadju.Checked Then
            b = 1
        End If
        If cbcan.Checked Then
            c = 1
        End If
        If cbadmin.Checked Then
            d = 1
        End If

        cmd.Parameters("@g") = cbogroup.SelectedValue
        Dim co = cmd.ExecuteScalar("SELECT COUNT(User_Type_id) FROM User_Role_Group WHERE User_Type_id=@g")

        cmd.Parameters("@group") = cbogroup.SelectedValue
        cmd.Parameters("@emp") = a
        cmd.Parameters("@adj") = b
        cmd.Parameters("@can") = c
        cmd.Parameters("@adm") = d

        If l = 0 Then

            If co > 0 Then
                MessageBox.Show("This Group already assign Menu you cannot assign new, but you can update")
                Exit Sub
            End If

            cmd.ExecuteNonQuery("INSERT INTO user_role_group VALUES(@group,@emp,@adj,@can,@adm)")
            MessageBox.Show("Assign New Group Menu Success")
        Else
            cmd.Parameters("@rid") = l
            cmd.ExecuteNonQuery("Update User_Role_Group SET user_type_id=@group,Employee=@emp,Adjust=@adj,Candidate=@can,Admin=@adm WHERE user_role_id=@rid")
            MessageBox.Show("Update Success")
        End If
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub
End Class